package servlet;

import dao.CartDao;
import dao.UserDao;
import entity.Cart;
import entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/deleteCartById")
public class MyCartDeleteServlet extends HttpServlet {
	@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        doGet(request, response);
    }
	@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		Integer cartId = Integer.parseInt(request.getParameter("cartId"));


		CartDao dao = new CartDao();
		int ret = dao.deleteByIdCart(cartId);
		if(ret == -1) {
			response.sendRedirect("error.jsp");
			return;
		}else {
			request.getSession().setAttribute("successMsg", "删除成功！");
			User user = (User) request.getSession().getAttribute("user");
			Integer userId = user.getUserId();
			List<Cart> myCartList = dao.selectMyCart(userId);
			request.getSession().setAttribute("myCartList",myCartList);
			response.sendRedirect("frontend/cart.jsp");
		}

	}
}
